function startWatch() {
	var options = { frequency: 250 };
	watchID = navigator.accelerometer.watchAcceleration(onWASuccess, onWAError, options);
}
function onWASuccess(acceleration) {
	if(Math.abs(acceleration.x)>13 || Math.abs(acceleration.y)>13  ||Math.abs(acceleration.z)>13){
	   nowTime = new Date().getTime();
	   if(lastShakeTime==null||nowTime-lastShakeTime>1000){
		   $("#shakeUdlsUl").css("disply","none");
			playAudio("/android_asset/www/media/shake1.mp3");
			$("img[src='images/yaoyyg.gif']").attr("src","images/yaoyyg.gif");
			getRandomUdls();
	   }
	   lastShakeTime=nowTime;
    }
}
function getRandomUdls(){
	$.ajax({
		url :interfaceMobileUrl+"/release/random.json?userid="+userid+"&key="+userkey,
		error:function(){
			playAudio("/android_asset/www/media/shake_nomatch.mp3");
			xfAlert("呵呵，竟然失手了");
		},
		success:function(response){
			playAudio("/android_asset/www/media/shake_match.mp3");
			$("#shakeUdlsUl li h3").html(response.title);
			$("#shakeUdlsUl li").find("p").eq(0).html(response.author);
			$("#shakeUdlsUl li p").find("span").eq(1).html(response.detail);
			$("#shakeUdlsUl li img").attr("isbn",response.isbn).attr("src","images/default1.jpg");
			$("#shakeUdlsUl").css("display","block");
			coverCover();
			$("#shakeUdlsUl a[href='#zjxq']").unbind("click");
			$("#shakeUdlsUl a[href='#zjxq']").click(function(){
				$("img#selectedPic").attr("src",assetsUrl+"images/default1.jpg");
				$("img#selectedPic").attr("isbn",response.isbn);
				coverCover();
				$("#bookDetailTitle").html(response.title);
				$("#bookDetailAuthor").html(response.author);
				$("#bookDetailAddress").html(response.district+response.detail);
				commentNum(response.isbn);
				//otherInfo
				$(".otherTitle").html("书名："+response.title);
				$(".otherAuthor").html("作者："+response.author);
				if(response.publisher==undefined){
					response.publisher ="暂无信息";
				}
				$(".otherPublisher").html("出版社："+response.publisher);
				$(".otherIsbn").html("isbn："+response.isbn);
				if(response.class0==undefined){
					response.class0="暂无信息";
				}
				$(".otherClass").html("类型："+response.class0);
				//
				$("#toBookSubDetail").attr("isbn",response.isbn);
//				bookSumary(response.isbn,$("#contentText"));
				//userDetail
				$("#userDetailNickname").html(response.userNickname);
				$("#userDetailPhone").html(response.phone);
				$("#userDetailQQ").html(response.qq);
				
				$("#toUserDetail").attr("userid",response.userid);
				$("#toUserDetail").attr("rid",response.id);
			});
		}
	});
}

function playAudio(Audio) {
	my_media = new Media(Audio,function(){},function(){});
	my_media.play();
}
function stopWatch() {
	if (watchID) {
		navigator.accelerometer.clearWatch(watchID);
		watchID = null;
	}
}

function onWAError() {
	xfAlert("加载摇一摇失败");
//	navigator.notification.alert('onError!');
}
$(function(){
	$("a[href='#yaoyiy']").click(function(){
		if(userid ==null){
			xfAlert("请先登录");
			$.ui.loadContent("#denglu",false,false,"up");
			return false;
		}
		
	});
	
});
function yaoyiyLoadedPanel() {
	lastShakeTime = null;
	startWatch();
}
function yaoyiyUnloadedPanel() {
	stopWatch();
}